package com.weitu.emergency.service.impl;

import com.weitu.emergency.entity.RehearsalUser;
import com.weitu.emergency.mapper.RehearsalUserMapper;
import com.weitu.emergency.service.RehearsalUserService;
import com.weitu.permission.user.entity.User;
import com.weitu.permission.user.entity.UserDepartment;
import com.weitu.permission.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author zhangqingxi 2017-05-10
 */
@Service
public class RehearsalUserServiceImpl implements RehearsalUserService {

    private final RehearsalUserMapper rehearsalUserMapper;
    private final UserMapper userMapper;

    @Autowired
    public RehearsalUserServiceImpl(RehearsalUserMapper rehearsalUserMapper, UserMapper userMapper) {
        this.rehearsalUserMapper = rehearsalUserMapper;
        this.userMapper = userMapper;
    }

    @Override
    public List<RehearsalUser> findByParam(Map paramMap) {
        return rehearsalUserMapper.findByParam(paramMap);
    }

    @Override
    public Integer findByParamCount(Map paramMap) {
        return rehearsalUserMapper.findByParamCount(paramMap);
    }

    @Override
    public List<RehearsalUser> findByRehearsalPlanId(String rehearsalPlanId) {
        List<RehearsalUser> rehearsalUserList=rehearsalUserMapper.findByRehearsalPlanId(rehearsalPlanId);
        for (RehearsalUser user:rehearsalUserList){

            User sysUser= userMapper.selectById(user.getUserId());
            String userName=sysUser.getRealName();
            StringBuffer sb=new StringBuffer();
            List<UserDepartment> userDepartmentList=sysUser.getUserDepartmentList();
            for (int i=0;i<userDepartmentList.size();i++){
                sb.append(userDepartmentList.get(i).getDeptName());
                if (i<userDepartmentList.size()-1){
                    sb.append(",");
                }
            }
            user.setUserName(userName);
            user.setDeptName(sb.toString());

        }
        return rehearsalUserList;
    }

}
